The Kernel Kit Table of Contents     The Kernel Kit Index

Semaphores

Declared in: be/kernel/OS.h

Library: libroot.so

A semaphore is a token that's used to synchronize multiple threads. The semaphore concept is simple: To enter into a semaphore-protected "critical section", a thread must first "acquire" the semaphore, through the acquire_sem() function. When it passes out of the critical section, the thread "releases" the semaphore through release_sem().

The advantage of the semaphore system is that if a thread can't acquire a semaphore (because the semaphore is yet to be released by the previous acquirer), the thread blocks in the acquire_sem() call. While it's blocked, the thread doesn't waste any cycles.

For the full story about semaphores, see "Semaphore Concepts". For some code examples, see "Semaphore Examples".


Semaphore Functions


acquire_sem() , acquire_sem_etc()

                                                         
  

status_t acquire_sem(sem_id sem)

status_t acquire_sem_etc(sem_id sem,
      uint32 count,
      uint32 flags,
      bigtime_t timeout)

These functions attempt to acquire the semaphore identified by the sem argument. Except in the case of an error, acquire_sem() doesn't return until the semaphore has actually been acquired.

acquire_sem_etc() is the full-blown acquisition version: It's essentially the same as acquire_sem(), but, in addition, it lets you acquire a semaphore more than once, and also provides a timeout facility:

 
The Kernel Kit defines two other semaphore-acquisition flag constants (B_CAN_INTERRUPT and B_CHECK_PERMISSION). These additional flags are used by device drivers—adding these flags into a "normal" (or "user-level") acquisition has no effect. However, you should be aware that the B_CHECK_PERMISSION flag is always added in to user-level semaphore acquisition in order to protect system-defined semaphores.


Other than the timeout and the acquisition count, there's no difference between the two acquisition functions. Specifically, any semaphore can be acquired through either of these functions; you always release a semaphore through release_sem() (or release_sem_etc()) regardless of which function you used to acquire it.

To determine if the semaphore is available, the function looks at the semaphore's thread count (before decrementing it):

RETURN CODES

The other return values apply to acquire_sem_etc() only:


create_sem()

                                                         
  

sem_id create_sem(uint32 thread_count, const char *name)

Creates a new semaphore and returns a system-wide sem_id number that identifies it. The arguments are:

Valid sem_id numbers are positive integers. You should always check the validity of a new semaphore through a construction such as

   if ((my_sem = create_sem(1,"My Semaphore")) < B_OK)
      /* If it>s less than B_NO_ERROR, my_sem is invalid. */

create_sem() sets the new semaphore's owner to the team of the calling thread. Ownership may be re-assigned through the set_sem_owner() function. When the owner dies (when all the threads in the team are dead), the semaphore is automatically deleted. The owner is also signficant in a delete_sem() call: Only those threads that belong to a semaphore's owner are allowed to delete that semaphore.

RETURN CODES


delete_sem()

                                                         
  

status_t delete_sem(sem_id sem)

Deletes the semaphore identified by the argument. If there are any threads waiting in the semaphore's thread queue, they're immediately unblocked.

 
This function may only be called from a thread that belongs to the semaphore's owner.


RETURN CODES


get_sem_count()

                                                         
  

status_t get_sem_count(sem_id sem, int32 *thread_count)

 
For amusement purposes only; never predicate your code on this function.


Returns, by reference in thread_count, the value of the semaphore's thread count variable:

By the time this function returns and you get a chance to look at the thread_count value, the semaphore's thread count may have changed. Although watching the thread count might help you while you're debugging your program, this function shouldn't be an integral part of the design of your application.

RETURN CODES


get_sem_info() , get_next_sem_info()

                                                         
  

status_t get_sem_info(sem_id sem, sem_info *info)

status_t get_next_sem_info(team_id team,
      uint32 *cookie,
      sem_info *info)

Copies information about a particular semaphore into the sem_info structure designated by info. The first version of the function designates the sempahore directly, by sem_id.

The get_next_sem_info() version lets you step through the list of a team's semaphores through iterated calls on the function. The team argument identifies the team you want to look at; a team value of 0 means the team of the calling thread. The cookie argument is a placemark; you set it to 0 on your first call, and let the function do the rest. The function returns B_BAD_VALUE when there are no more sempahores to visit:

   /* Get the sem_info for every sempahore in this team. */
   sem_info info;
   int32 cookie = 0;
   
   while (get_next_sem_info(0, &cookie, &info) == B_OK)
      ...

RETURN CODES


release_sem() , release_sem_etc()

                                                         
  

status_t release_sem(sem_id sem)

status_t release_sem_etc(sem_id sem, int32 count, uint32 flags)

The release_sem() function de-queues the thread that's waiting at the head of the semaphore's thread queue (if any), and increments the semaphore's thread count. release_sem_etc() does the same, but for count threads.

Normally, releasing a semaphore automatically invokes the kernel's scheduler. In other words, when your thread calls release_sem(), you're pretty much guaranteed that some other thread will be switched in immediately afterwards, even if your thread hasn't gotten its fair share of CPU time. If you want to subvert this automatism, call release_sem_etc() with a flags value of B_DO_NOT_RESCHEDULE. Preventing the automatic rescheduling is particularly useful if you're releasing a number of different semaphores all in a row: By avoiding the rescheduling you can prevent some unnecessary context switching.

RETURN CODES

See also: acquire_sem()


set_sem_owner()

                                                         
  

status_t set_sem_owner(sem_id sem, team_id team)

Transfers ownership of the designated semaphore to team. A semaphore can only be owned by one team at a time; by setting a semaphore's owner, you remove it from its current owner.

There are no restrictions on who can own a semaphore, or on who can transfer ownership. In practice, however, the only reason you should ever transfer ownership is if you're writing a device driver and you need to bequeath a semaphore to the kernel (the team of which is known, for this purpose, as B_SYSTEM_TEAM).

Semaphore ownership is meaningful for two reason:

To discover a semaphore's owner, use the get_sem_info() function.

RETURN CODES


Semaphore Structures and Types


sem_id

                                                         
  

typedef int32 sem_id;

sem_id numbers identify semaphores. The id is assigned when the semaphore is created (create_sem()). The values are unique across the system.


sem_info

                                                         
  

typedef struct sem_info {
      sem_id sem;
      team_id team;
      char name[B_OS_NAME_LENGTH];
      int32 count;
      thread_id latest_holder;
      }

The sem_info structure supplies information about a semaphore. You retrieve the structure through the get_sem_info() function. The information in the sem_info structure is guaranteed to be internally consistent, but the structure as a whole should be consider to be out-of-date as soon as you receive it. It provides a picture of a semaphore as it exists just before the info-retrieving function returns.

The fields are:

 
The lastest_holder field is highly undependable; in some cases, the kernel doesn't even record the semaphore acquirer. Although you can use this field as a hint while debugging, you shouldn't take it too seriously. Love, Mom.



Semaphore Constants


Semaphore Control Flags


The Kernel Kit Table of Contents     The Kernel Kit Index


The Be Book,
...in lovely HTML...
for BeOS Release 5.

Copyright © 2000 Be, Inc. All rights reserved..